package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.entity.Result;
import com.itheima.mapper.InterventionMapper;
import com.itheima.mapper.KnowledgeFoodMapper;
import com.itheima.mapper.KnowledgeSportMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Intervention;
import com.itheima.pojo.KnowledgeFood;
import com.itheima.pojo.KnowledgeSport;
import com.itheima.service.InterventionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = InterventionService.class)
@Transactional
public class InterventionServiceImpl implements InterventionService {
    @Autowired
    private InterventionMapper interventionMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private KnowledgeFoodMapper knowledgeFoodMapper;
    @Autowired
    private KnowledgeSportMapper knowledgeSportMapper;
    @Override
    public void add(Intervention intervention) {
        //取出前端数据
        interventionMapper.add(intervention);
        //获取健康干预项id
        int interId = intervention.getId();
        List<Integer> foodIds = intervention.getKnowledgeFoodIds();
        List<Integer> sportIds = intervention.getKnowledgeSportIds();
        for (Integer foodId : foodIds) {
            Map map = new HashMap();
            map.put("foodId",foodId);
            map.put("interId",interId);
            knowledgeFoodMapper.addAssociation(map);
        }
        for (Integer sportId : sportIds) {
            Map map = new HashMap();
            map.put("sportId",sportId);
            map.put("interId",interId);
            knowledgeSportMapper.addAssociation(map);
        }

    }

    @Override
    public Result findIntervention(Integer id) {
        Intervention intervention = interventionMapper.findById(id);
        if(intervention == null){
            return new Result(false,"尚未进行健康干预, 无法更新",false);
        }
        int interId = intervention.getId();
        List<Integer> sportIds = knowledgeSportMapper.findIdsByInterId(interId);
        List<Integer> foodIds = knowledgeFoodMapper.findIdsByInterId(interId);
        //根据ids获取相应的对象
        List<KnowledgeFood> knowledgeFoods = new ArrayList<>();
        List<KnowledgeSport> knowledgeSports = new ArrayList<>();

        for (Integer sportId : sportIds) {
            KnowledgeSport knowledgeSport = knowledgeSportMapper.findById(sportId);
            //将对象放入集合
            knowledgeSports.add(knowledgeSport);
        }
        for (Integer foodId : foodIds) {
            KnowledgeFood knowledgeFood = knowledgeFoodMapper.findById(foodId);
            knowledgeFoods.add(knowledgeFood);
        }

        intervention.setKnowledgeFoods(knowledgeFoods);
        intervention.setKnowledgeSports(knowledgeSports);
        intervention.setKnowledgeSportIds(sportIds);
        intervention.setKnowledgeFoodIds(foodIds);
        if(intervention.getDoctorAdvice() == null){
            intervention.setDoctorAdvice("");
        }
        return new Result(true,"查询健康干预成功",intervention);
    }

    @Override
    public void edit(Intervention intervention) {
        //取出前端数据
        interventionMapper.edit(intervention);
        //获取健康干预项id
        int interId = intervention.getId();
        //根据id删除之前的外键联系
        interventionMapper.deleteFoodAssociation(interId);
        interventionMapper.deleteSportAssociation(interId);
        System.out.println(interId);
        //根据前端数据重新添加新的外键关系
        List<Integer> foodIds = intervention.getKnowledgeFoodIds();
        List<Integer> sportIds = intervention.getKnowledgeSportIds();
        for (Integer foodId : foodIds) {
            Map map = new HashMap();
            map.put("foodId",foodId);
            map.put("interId",interId);
            knowledgeFoodMapper.addAssociation(map);
        }
        for (Integer sportId : sportIds) {
            Map map = new HashMap();
            map.put("sportId",sportId);
            map.put("interId",interId);
            knowledgeSportMapper.addAssociation(map);
        }
    }


}
